package com.sy.service.impl;

import com.sy.dao.CarDao;
import com.sy.dao.impl.CarDaoImpl;
import com.sy.dto.Page;
import com.sy.dto.Result;
import com.sy.entity.Car;
import com.sy.service.CarService;
import com.sy.vo.CarSearchParams;

import java.util.Objects;

import static com.sy.constant.BusinessConstant.DEFAULT_PAGE_NO;
import static com.sy.constant.BusinessConstant.DEFAULT_PAGE_SIZE;

public class CarServiceImpl extends AbstractBaseServiceImpl implements CarService {
    private CarDao carDao = new CarDaoImpl();

    @Override
    public Page<Car> getCarsByConditions(CarSearchParams params) {
        Page<Car> page = new Page<>();
        Integer pageNo = params.getPageNo();
        page.setPageNo(Objects.isNull(pageNo) ? DEFAULT_PAGE_NO : pageNo);
        page.setPageSize(DEFAULT_PAGE_SIZE);
        Long total = carDao.getCarsCountByConditions(params);
        page.setTotal(total);
        page.setPageCount((long) Math.ceil(total * 1.0 / DEFAULT_PAGE_SIZE));
        Integer start = (pageNo - 1) * DEFAULT_PAGE_SIZE;
        page.setData(carDao.getCarsByConditions(params, start));
        return page;
    }
}
